package cn.hiapi.shopkeeper.service.activity;

import cn.hiapi.basic.jpa.pageable.QueryWrapper;
import cn.hiapi.basic.service.AbsBasicService;
import cn.hiapi.shopkeeper.entity.activity.ActivityCard;
import cn.hiapi.shopkeeper.entity.activity.ActivityCodeRecord;
import cn.hiapi.shopkeeper.entity.order.ActivityOrder;
import cn.hiapi.shopkeeper.jpa.activity.ActivityCodeRecordJpa;
import cn.hiapi.utils.RandomUtils;
import com.alibaba.fastjson.JSONObject;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author AdinZ
 * @date 2023/1/1 22:57
 */
@Service
public class ActivityCodeRecordService extends AbsBasicService<ActivityCodeRecordJpa, ActivityCodeRecord, Long> {


    /**
     * 创建核销码
     *
     * @param oid
     * @param cardId
     * @param card
     */
    @Transactional
    public void createCode(long oid, long cardId, long uid, ActivityCard card) {
        List<ActivityCodeRecord> records = new ArrayList<>();
        for (int i = 0; i < card.getNumber(); i++) {
            ActivityCodeRecord record = new ActivityCodeRecord();
            record.setAid(card.getAid());
            record.setShopId(card.getShopId());
            record.setOid(oid);
            record.setUid(uid);
            record.setCardId(cardId);
            record.setCode(RandomUtils.randomSnowflake(""));
            records.add(record);
        }
        this.save(records);
    }

    @Transactional
    public void createCode(ActivityOrder order) {
        List<ActivityCodeRecord> records = new ArrayList<>();
        for (int i = 0; i < order.getNumber(); i++) {
            ActivityCodeRecord record = new ActivityCodeRecord();
            record.setAid(order.getAid());
            record.setShopId(order.getShopId());
            record.setOid(order.getId());
            record.setUid(order.getBuyerId());
            record.setCode(RandomUtils.randomSnowflake(""));
            records.add(record);
        }
        this.save(records);
    }


    public List<ActivityCodeRecord> findByOid(long oid) {
        return this.findList(new QueryWrapper().eq("oid", oid));
    }


    //转移卡密到新订单中
    @Transactional
    public void giftOrder(long sourceId, long targetId) {

        List<ActivityCodeRecord> records = findByOid(sourceId);


        List<ActivityCodeRecord> newRecords = new ArrayList<>();

        for (ActivityCodeRecord record : records) {
            ActivityCodeRecord copy = record.copy(new ActivityCodeRecord());
            copy.setId(null);
            copy.setOid(targetId);
            copy.setCode(RandomUtils.randomSnowflake(""));
            newRecords.add(copy);
        }


        records.forEach(e -> {
            if (e.getStatus() == 0) e.setStatus(3);
            e.setUpdated(System.currentTimeMillis());
        });

        this.save(records);
        this.save(newRecords);

    }

    public ActivityCodeRecord getCode(String code) {
        List<ActivityCodeRecord> list = this.findList(new QueryWrapper().eq("code", code));
        if (list.isEmpty()) return null;
        return list.get(0);
    }

    /**
     * 销毁
     *
     * @param oid
     */
    @Transactional
    public void destroyCode(long oid) {
        List<ActivityCodeRecord> list = this.findByOid(oid);
        list.forEach(e -> {
            if (e.getStatus() == 0) e.setStatus(3);
            e.setUpdated(System.currentTimeMillis());
        });
        this.save(list);
    }

    public List<JSONObject> totalCount(long oid) {
        List data= this.repository.totalCount(oid);
        return data;
    }
}
